import axios from "axios";

export default {
  state: {
    allCities: [],
  },
  getters: {},
  mutations: {},
  actions: {
    getAllCity(context, data) {
      let url = "/city/get/all";
      let postData = {};
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => {
          context.state.allCities = resp.data.data;
          data.success(context.state.allCities);
        })
        .catch((resp) => console.log(resp));
    },
    getProvinces(context, data) {
      let url = "/city/get/provinces";
      let postData = {};
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => data.success(resp))
        .catch((resp) => data.error(resp));
    },
    getCitiesByPid(context, data) {
      let url = "/city/get/city/by/pid";
      let postData = {
        pid: data.pid,
      };
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      //   console.log(data.token);
      axios
        .post(url, postData, config)
        .then((resp) => data.success(resp))
        .catch((resp) => data.error(resp));
    },
    addNewCity(context, data) {
      let url = "/city/add";
      let postData = {
        pid: data.pid,
        name: data.name,
      };
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => data.success(resp))
        .catch((resp) => data.error(resp));
    },
    updateCity(context, data) {
      let url = "/city/update";
      let postData = {
        id: data.id,
        pid: data.pid,
        name: data.name,
      };
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => data.success(resp))
        .catch((resp) => data.error(resp));
    },
    deleteCityById(context, data) {
      let url = "/city/delete/by/id";
      let postData = {
        id: data.id,
      };
      let config = {
        headers: {
          Authorization: "Bearer " + data.token,
        },
      };
      axios
        .post(url, postData, config)
        .then((resp) => data.success(resp))
        .catch((resp) => data.error(resp));
    },
  },
  modules: {},
};
